package com.codeing.demo.redis.redission.controller;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @auther ShiLei
 * @create 2020-09-15 14:48
 */
@RestController
public class GoodController
{
    public static final String REDIS_LOCK = "atguiguLock";
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //@Value("${server.port}")
    private String serverPort;

    @Autowired
    private Redisson redisson;

    @GetMapping("/buy_goods")
    public String buy_Goods() throws Exception
    {
        String value = UUID.randomUUID().toString()+Thread.currentThread().getName();

        RLock redissonLock = redisson.getLock(REDIS_LOCK);
        redissonLock.lock();

        try {

            String result = stringRedisTemplate.opsForValue().get("goods:001");
            int goodsNumber = result == null ? 0 : Integer.parseInt(result);

            if(goodsNumber > 0)
            {
                int realNumber = goodsNumber - 1;
                stringRedisTemplate.opsForValue().set("goods:001",String.valueOf(realNumber));
                System.out.println("成功买到商品，库存还剩下： "+realNumber+" 件"+"\t 服务提供端口 "+serverPort);
                return "成功买到商品，库存还剩下： "+realNumber+" 件"+"\t 服务提供端口 "+serverPort;
            }else{
                System.out.println("商品已经售完/活动结束/调用超时,欢迎下次光临 "+"\t 服务提供端口 "+serverPort);
            }
            return "商品已经售完/活动结束/调用超时,欢迎下次光临 "+"\t 服务提供端口 "+serverPort;
        } finally {
            if(redissonLock.isLocked() && redissonLock.isHeldByCurrentThread())
            {
                redissonLock.unlock();
            }
        }
    }

    @GetMapping("putRedisValue")
    public Integer put(){
        Integer count = 0;
        String str = "{\"@class\":\"com.yjyz.erp.settings.srv.dto.notice.ErpNoticeDto\",\"noticeId\":633493993856356480,\"cityCode\":450100,\"cityName\":\"南宁\",\"publisherId\":592912728619143296,\"publisherUserId\":577699161363415040,\"brandId\":565998180494215232,\"branchId\":552692373611008128,\"subject\":\"标题1\",\"noticeType\":56,\"attachments\":[\"java.util.ArrayList\",[{\"@class\":\"com.yjyz.erp.settings.srv.po.Attach\",\"bucket\":\"123\",\"name\":\"123\",\"size\":10000,\"type\":\"1231\",\"url\":\"12312\"}]],\"content\":\"正文正文\\n&#x3c;p&#x3e;这里有标签&#x3c;/p&#x3e;\",\"noticeStatus\":1,\"receiverId\":[\"java.util.ArrayList\",[{\"@class\":\"com.yjyz.erp.settings.srv.po.notice.NoticeReceiverPo\",\"id\":591728590021120128,\"type\":1},{\"@class\":\"com.yjyz.erp.settings.srv.po.notice.NoticeReceiverPo\",\"id\":578657047846244480,\"type\":1}]],\"sendTm\":1574154846749,\"listTm\":1574154846749,\"isReserve\":0,\"msgId\":\"\",\"updatedBy\":577699161363415040,\"updatedTm\":1574154858216,\"createdBy\":577699161363415040,\"createdTm\":1574154847178,\"isFlag\":1,\"forceStatus\":0}";
        for (int i = 0; i <= 500000 ; i++) {
            stringRedisTemplate.opsForValue().set("erp:settings:srv:ErpNotice2::123" + i,str);
            count++;
        }
        return count;
    }
}

